/**
 * Notes: 管理员业务逻辑 (新版API)
 * Ver : WeCollege Server API 1.0.0
 * Date: 2025-01-27
 * Description: 使用自建服务器API重构管理员业务逻辑
 */

const BaseBiz = require('./base_biz.js');
const cacheHelper = require('../../helper/cache_helper.js');
const serverHelper = require('../../helper/cloud_helper.js'); // 使用替换后的helper
const pageHelper = require('../../helper/page_helper.js');
const helper = require('../../helper/helper.js');
const constants = require('../constants.js');

class AdminBiz extends BaseBiz {

  // 获取管理员token  
  static getAdminToken() {
    let admin = cacheHelper.get(constants.CACHE_ADMIN);
    return admin || null;
  }

  // 设置管理员token
  static setAdminToken(admin) {
    if (!admin) return;
    cacheHelper.set(constants.CACHE_ADMIN, admin, constants.CACHE_ADMIN_EXPIRE);
  }

  // 获取管理员ID
  static getAdminId() {
    let admin = cacheHelper.get(constants.CACHE_ADMIN);
    if (!admin) return '';
    return admin.id || '';
  }

  // 获取管理员名称
  static getAdminName() {
    let admin = cacheHelper.get(constants.CACHE_ADMIN);
    if (!admin) return '';
    return admin.name || '';
  }

  // 是否管理员登录
  static isAdminLogin() {
    let id = AdminBiz.getAdminId();
    return (id.length > 0) ? true : false;
  }

  // 管理员登录
  static async adminLogin(name, password) {
    let params = {
      name: name,
      password: password
    };

    let opt = {
      title: '登录中'
    };

    try {
      const result = await serverHelper.callCloudSumbit('admin/login', params, opt);
      
      if (result && result.data && result.data.token) {
        // 构建管理员信息
        const adminInfo = {
          id: result.data.admin.adminId || result.data.admin.id,
          name: result.data.admin.name || result.data.admin.adminName,
          token: result.data.token,
          type: result.data.admin.type || 1,
          status: result.data.admin.status || 1
        };

        AdminBiz.setAdminToken(adminInfo);
        return adminInfo;
      }

      throw new Error(result.message || '登录失败');
    } catch (err) {
      console.error('管理员登录失败:', err);
      throw err;
    }
  }

  // 管理员登录检查
  static async adminLoginCheck(that = null) {
    let admin = cacheHelper.get(constants.CACHE_ADMIN);
    if (admin && admin.token) {
      if (that) {
        that.setData({
          isAdminLogin: true,
          adminName: admin.name
        });
      }
      return true;
    }

    // 跳转到管理员登录页面
    wx.reLaunch({
      url: pageHelper.fmtURLByPID('/pages/admin/index/login/admin_login'),
    });
    return false;
  }

  // 清除管理员登录缓存
  static clearAdminToken() {
    cacheHelper.remove(constants.CACHE_ADMIN);
  }

  // 管理员退出登录
  static adminLogout() {
    AdminBiz.clearAdminToken();
    wx.reLaunch({
      url: pageHelper.fmtURLByPID('/pages/admin/index/login/admin_login'),
    });
  }

  // 获取仪表板数据
  static async getDashboardData() {
    return await serverHelper.callCloudData('admin/home', {});
  }

  // 清除推荐
  static async clearVouch() {
    return await serverHelper.callCloudSumbit('admin/clear_vouch', {});
  }

  // 获取用户列表
  static async getUserList(params = {}) {
    return await serverHelper.callCloudData('admin/user_list', params);
  }

  // 删除用户
  static async deleteUser(userId) {
    return await serverHelper.callCloudSumbit('admin/user_del', { userId });
  }

  // 设置用户状态
  static async setUserStatus(userId, status) {
    return await serverHelper.callCloudSumbit('admin/user_status', { 
      userId, 
      status 
    });
  }

  // 获取用户详情
  static async getUserDetail(userId) {
    return await serverHelper.callCloudData('admin/user_detail', { userId });
  }

  // 导出用户数据
  static async exportUserData(params = {}) {
    return await serverHelper.callCloudData('admin/user_data_get', params);
  }

  // 生成用户Excel
  static async generateUserExcel(params = {}) {
    return await serverHelper.callCloudData('admin/user_data_export', params);
  }

  // 清除导出数据
  static async clearExportData() {
    return await serverHelper.callCloudData('admin/user_data_del', {});
  }
}

/** 表单校验 */
AdminBiz.CHECK_FORM = {
  name: 'formName|must|string|min:1|max:30|name=管理员名称',
  password: 'formPassword|must|string|min:6|max:20|name=密码'
};

module.exports = AdminBiz;